#!/usr/bin/python3
# -*- coding: utf-8 -*-

import requests
import json

from multiprocessing import Pool, freeze_support
import sys
import codecs
import logging


base_url = 'https://www.engineeringvillage.com'
initial_url = base_url + '/search/submit.url'
next_url = base_url + '/search/results/quick.url'


def get_authors_from_conference(
        conference_abbreviation, conference_name, startyear, endyear):
    """ Return authors from a conference. """

    initial_payload = {
        'usageOrigin': 'searchform',
        'usageZone': 'quicksearch',
        'editSearch': '',
        'isFullJsonResult': 'true',
        'angularReq': 'true',
        'CID': 'searchSubmit',
        'searchtype': 'Quick',
        'origin': 'searchform',
        'category': 'quicksearch',
        'section1': 'CF',
        'searchWord1': conference_name,
        'database': '1',
        'yearselect': 'yearrange',
        'startYear': startyear,
        'endYear': endyear,
        'updatesNo': '1',
        'language': 'NO-LIMIT',
        'doctype': 'NO-LIMIT',
        'sort': 'relevance',
        'treatmentType': 'NO-LIMIT',
        'pageSize': '1000'

        #'resetDataBase': '1',
    }
    r = requests.post(initial_url, params=initial_payload)
    j = json.loads(r.text)

    search_id = j['searchMetaData']['searchId']
    results_count = j['pagenav']['resultscount']
    results_per_page = j['pagenav']['resultsperpage']

    author_list = []
    current_index = 1

    while current_index <= results_count:

        if current_index == 1:
            pass
        else:
            next_payload = {
                'navigator': 'NEXT',
                'SEARCHID': search_id,
                'database': '1',
                'angularReq': 'true',
                'isFullJsonResult': 'false',
                'usageOrigin': 'searchresults',
                'COUNT': str(current_index),
                'usageZone': 'nextpage'
            }
            r = requests.post(next_url, params=next_payload)
            j = json.loads(r.text)

        for result in j['results']:
            year = result['yr']
            title = result['title']
            pages = result['pages']
            if result['authors'] is None:
                print('[Warning] skip "%s" without any author' % title, file=sys.stderr)
                continue
            for author_index, author in enumerate(result['authors']):
                author = result['authors'][author_index]
                name = author['name']
                affiliation = author['affils'][0]['name'] if author['affils'] is not None else ''
                country = ''
                email = author['email']
                author_item = [conference_abbreviation, year, title, pages,
                        name, str(author_index+1), affiliation, country, email]
                author_list.append(author_item)

        current_index += results_per_page

    return author_list


def main():
    if len(sys.argv) <= 1:
        print('Usage: EV.py <conference-list> [start-year] [end-year]', file=sys.stderr)
        return

    filename = sys.argv[1]
    startyear = sys.argv[2] if len(sys.argv) > 2 else '2015'
    endyear = sys.argv[3] if len(sys.argv) > 3 else startyear

    logging.basicConfig(level=logging.INFO)

    uopen = lambda path, mode: open(path, mode, encoding='utf-8')
    requests.session().cookies.clear()
    for line in uopen(filename, 'r'):
        abbr, name = line.split('\t')
        print('', file=sys.stderr)
        print('[Working] extract %s papers between %s and %s' % (abbr, startyear, endyear), file=sys.stderr)
        author_list = get_authors_from_conference(abbr, name, startyear, endyear)
        if len(author_list) == 0:
            print('[Warning] no %s papers' % abbr, file=sys.stderr)
        # Output csv format: abbr, year, title, pages, author, order, affiliation, country, email
        with open('%s.csv' % abbr, 'wb') as csv:  # for excel (byte mode + BOM)
            csv.write(codecs.BOM_UTF8)
            add_quote = lambda x: '"%s"' % x if ',' in x else x
            csv.write('\n'.join(
                ','.join(add_quote(field) for field in author_info)
                for author_info in author_list).encode('utf-8'))


if __name__ == '__main__':
    freeze_support()
    main()
